package com.smday.fireworks.config;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * swagger ui
 * created by Summer-day
 */
@EnableSwagger2
@Configuration
public class Swagger2Config {

  @Autowired
  private FireworksConfig fireworksConfig;

  /**
   * 创建API
   * @return
   */
  @Bean
  public Docket createRestApi() {
    return new Docket(DocumentationType.SWAGGER_2)
      .apiInfo(apiInfo())
      .select()
      //为当前包下controller生成API文档
      .apis(RequestHandlerSelectors.basePackage("com.smday.fireworks.controller"))
      //为有@Api注解的Controller生成API文档
      .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
      //为有@ApiOperation注解的方法生成API文档
      .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
      .paths(PathSelectors.any())
      .build()
      .securitySchemes(securitySchemes())
      .securityContexts(securityContexts());
  }

  /**
   * 设置安全上下文
   * @return
   */
  private List<SecurityContext> securityContexts() {
    //设置需要登录认证的路径
    List<SecurityContext> result = new ArrayList<>();
    result.add(getContextByPath("/admin/.*"));

    return result;
  }

  /**
   * 需要认证的路径
   * @param pathRegex
   * @return
   */
  private SecurityContext getContextByPath(String pathRegex) {
    return SecurityContext.builder()
      .securityReferences(defaultAuth())
      .forPaths(PathSelectors.regex(pathRegex))
      .build();
  }

  /**
   * 默认的安全引用
   * @return
   */
  private List<SecurityReference> defaultAuth() {
    List<SecurityReference> result = new ArrayList<>();
    AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
    AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
    authorizationScopes[0] = authorizationScope;
    result.add(new SecurityReference("Authorization", authorizationScopes));
    return result;
  }

  /**
   * 安全模式,这里指定token通过Authrization请求头传递
   * @return
   */
  private List<? extends SecurityScheme> securitySchemes() {
    //设置请求头信息
    List<ApiKey> result = new ArrayList<>();
    ApiKey apiKey = new ApiKey("Authorization", "Authorization", "header");
    result.add(apiKey);
    return result;
  }

  /**
   * 添加摘要信息
   * @return
   */
  private ApiInfo apiInfo() {
    return new ApiInfoBuilder()
      .title("标题:Fireworks个人网站系统接口文档")
      .description("fireworks个人网站:用户模块,后台管理模块,收藏模块,日志模块,安全模块")
      .contact(new Contact(fireworksConfig.getName(), "https://www.hyhwky.com", "1332790762@qq.com"))
      .version("版本号: "+fireworksConfig.getVersion())
      .build();
  }
}
